package com.fast.admin.controller.sys;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fast.admin.entity.sys.Resource;
import com.fast.admin.service.sys.IResourceService;
import com.fast.admin.vo.LayXtreeVo;
import com.fast.common.base.BaseController;
import com.fast.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title：资源菜单控制器
 * @author：ylc
 * @date：2018/7/20 16:24
 * @version：1.0
 **/
@RequestMapping(value = "/sys/resource")
@Controller
public class ResourceController extends BaseController {

    @Autowired
    private IResourceService resourceService;

    @Override
    protected String getPrefix() {
        return "sys/resource/";
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public R save(Resource resource) {
        resourceService.addOrUpdate(resource);
        return R.ok();
    }

    /**
     * 获取下拉菜单树
     *
     * @return
     */
    @GetMapping(value = "/getSelectMenuTree")
    @ResponseBody
    public R getSelectMenuTree() {
        return R.ok(resourceService.findSelectMenuTree());
    }

    @GetMapping(value = "/getResourceTree")
    @ResponseBody
    public R getResourceTree() {
        return R.ok(resourceService.findResourceTree());
    }

    /**
     * 获取资源信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public R get(@PathVariable("id") Long id) {
        return R.ok(resourceService.selectById(id));
    }

    @PostMapping(value = "/del")
    @ResponseBody
    public R del(String id) {
        resourceService.delById(id);
        return R.ok();
    }

    /**
     * 通过角色获取所有资源并根据角色去选中
     *
     * @return
     */
    @GetMapping(value = "getLayXtreeResourceByRole")
    @ResponseBody
    public R getLayXtreeResourceByRole(Long roleId) {
        return R.ok(resourceService.getLayXtreeByRole(roleId));
    }

}
